const Settings = require("./src/config/settings.js").config;
const version = "0.0.1";
const outDir = "addons/renren_shop/public/static/dist/shop_sale_merge/";
const path = require("path");
const myPlugin = require("./webpackPlugins.js");
var CopyWebpackPlugin = require("copy-webpack-plugin");
let plugins = [];
function fmtUrl(url = "") {
  return url.replace(".[contenthash:8]", "");
}
function resolve(dir) {
  return path.join(__dirname, dir);
}

if (process.env.UNI_PLATFORM == "h5") {
  plugins.push(
    new CopyWebpackPlugin([
      {
        from: resolve("./src/static"),
        to: resolve("./dist/build/h5/" + outDir),
      },
    ])
  );
  // plugins.push(
  //     new BundleAnalyzerPlugin({
  //         analyzerPort:9999
  //     })
  // )
  if (process.env.NODE_ENV != "development") {
    plugins.push(new myPlugin());
  }
}



// console.log(Settings,'---->')
module.exports = {
  transpileDependencies: ["uni-simple-router"],
  publicPath: "/",
  assetsDir: outDir,
  filenameHashing: true,
  productionSourceMap: false,

  configureWebpack: {
        plugins: plugins,
        externals:{
            // 'vue': 'Vue',
            // 'vuex': 'Vuex',
            // 'axios': 'axios',
        }
    },

  devServer: {
    host: "0.0.0.0",
    port: 9000,
    https: false,
    hotOnly: false,
    proxy: {
      "/manageApi": {
        target: Settings.base_url,
        pathRewrite: { "/manageApi": "" },
        changeOrigin: true,
        cookieDomainRewrite: {
          "*": "0.0.0.0",
        },
      },
    },
  },
  chainWebpack: (config) => {
    config.plugins.delete("prefetch");

    let filename = "",
      chunkFilename = "";

    if (process.env.UNI_PLATFORM == "h5") {
      config.plugins.delete("named-chunks");
      config.plugins.delete("pwa");
      filename = fmtUrl(config.output.store.get("filename")) + "?v=" + version;
      chunkFilename =
        fmtUrl(config.output.store.get("chunkFilename")) + "?v=" + version;
    } else {
      filename = config.output.store.get("filename") + "?v=" + version;
      chunkFilename =
        config.output.store.get("chunkFilename") + "?v=" + version;
    }

    config.output.store.set("filename", outDir + filename);
    config.output.store.set("chunkFilename", outDir + chunkFilename);
    config.resolve.alias.set("@", resolve("./src"));
    config.module
      .rule("img")
      .test(/\.(png|jpe?g|gif|webp)(\?.*)?$/)
      .use("url-loader")
      .loader("file-loader")
      .options({
        name: outDir + "image/[name].[ext]",
      });
    config.module
      .rule("svg")
      .test(/\.(svg)(\?.*)?$/)
      .use("file-loader")
      .loader("file-loader")
      .options({
        name: outDir + "image/[name].[ext]",
      });

    const oneOfsMap = config.module.rule("scss").oneOfs.store;
    oneOfsMap.forEach((item) => {
      item
        .use("sass-resources-loader")
        .loader("sass-resources-loader")
        .options({
          // 公用scss
          resources: "./src/static/scss/public.scss",
        })
        .end();
    });
  },
};
